package com.kn

//import groovy.time

class FeePeriod {
    Date valueDate
    def startDate
    def endDate
    def daysInPeriod
    def longDescription
    def shortDescription

    public FeePeriod(Date value) {
        this.valueDate = value;
        init();
    }
    public GregorianCalendar getStartDate() { return startDate; }
    public void setStartDate(Date value) { this.startDate = value; }

    public GregorianCalendar getEndDate() { return endDate; }
    public void setEndDate(Date value) { this.endDate = value; }

    public groovy.time.Duration getDaysInPeriod() { return daysBetween(this.startDate, this.endDate); }

    public String getLongDescription() {
        return longDescription;
    }
    public String getShortDescription() {
        return shortDescription;
    }

    def init() {
        int selectedMonth = -1
        int selectedYear = -1
        def tempYear = 0

        selectedMonth = this.valueDate?.month + 1
        selectedYear = this.valueDate?.year + 1900
        if(selectedMonth > 11) {
            selectedMonth -= 12;
            selectedYear += 1;
  //          tempYear += 0
        }
        if(selectedMonth >= 0) {
            this.startDate = new GregorianCalendar()
            this.endDate = new GregorianCalendar()
            this.startDate.set(Calendar.MONTH, selectedMonth )
            this.startDate.set(Calendar.DAY_OF_MONTH, 1 )
            this.startDate.set(Calendar.YEAR, selectedYear)
            def tempMonth = this.startDate.get(Calendar.MONTH)

            tempMonth += 2  // add three months
            if(tempMonth > 11) {
                tempMonth -= 12
                tempYear += 1
            }
            endDate.set(Calendar.MONTH, tempMonth)
            endDate.set(Calendar.YEAR, this.startDate.get(Calendar.YEAR)+tempYear)
            endDate.set(Calendar.DAY_OF_MONTH, this.endDate.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) )

            this.daysInPeriod = daysBetween(this.startDate, this.endDate)
            this.longDescription = "${this.startDate?.format('MM/dd/yyyy')} Through ${this.endDate?.format('MM/dd/yyyy')}";
            this.shortDescription = "${this.startDate?.format('MM/dd/yy')} - ${this.endDate?.format('MM/dd/yy')}";
        }

//        println "FeePeriod"
//        println this.startDate
//        println this.endDate
//        println this.daysInPeriod
//        println this.longDescription
//        println this.shortDescription
    }

    def daysBetween(def startDate, def endDate) {
        use(groovy.time.TimeCategory) {
            def duration = endDate - startDate
            duration.days
        }
    }
}


